﻿using DevExpress.XtraEditors.Repository;
using DevTeam;
using Fatura;
using System;
using System.Data;
using System.Drawing;

namespace Fatura
{

    public partial class Faturalar : DevForm
    {
        public Faturalar()
        {
            InitializeComponent();
            this.Text = "Faturalar";
        }

        DataSet dsData = null;
        private void Faturalar_Load(object sender, EventArgs e)
        {
            dsData = ExecuteDataSet("FT_GetInvoice", string.Empty, null, null);
            gvMaster.Init(dsData.Tables[0]);
            gvMaster.AddDetailGrid(gvDetail, "InvoiceHeaderId");

            gvMaster.AddAutoIncrementalPK("InvoiceHeaderId");
            
            gvMaster.MakeColumnInvisible("CustomerId");

            gvMaster.FormatAsNumber(2, "TotalQty1", "TotalQty2", "TotalQty3", "SubTotal1", "SubTotal2", "SubTotal3", "SubTotal4", "SubTotal5");
            gvDetail.FormatAsNumber(2, "Quantity", "UnitPrice", "TotalValue");

            gvMaster.AddSummary("TotalQty1", "TotalQty2", "TotalQty3", "SubTotal1", "SubTotal2", "SubTotal3", "SubTotal4", "SubTotal5");
            gvDetail.AddSummary("Quantity", "UnitPrice", "TotalValue");

            gvMaster.OptionsView.NewItemRowPosition = gvDetail.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;

            bbiSave.ItemClick += bbiSave_ItemClick;
            bbiRefresh.ItemClick += bbiRefresh_ItemClick;
        }

        void bbiRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            
        }

        void bbiSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvMaster.EndEditing();
            gvDetail.EndEditing();
            foreach (DataRow drMaster in gvMaster.Update().Select("NewId is not null"))
            {
                dsData.Tables[0].Select("InvoiceHeaderId =" + drMaster["InvoiceHeaderId"])[0]["InvoiceHeaderId"] = drMaster["NewId"];
                foreach (DataRow drDetail in dsData.Tables[1].Select("InvoiceHeaderId =" + drMaster["InvoiceHeaderId"]))
                    drDetail["InvoiceHeaderId"] = drMaster["NewId"];
            }

            gvDetail.Update();
        }

        
    }
}
